﻿using System;
using System.Linq;
using System.Text;


using AFCEPF.BankApplication.BLL;
using AFCEPF.BankApplication.DAL;
using AFCEPF.BankApplication.Tests.Utilities;


using NUnit.Framework;


namespace AFCEPF.BankApplication.Tests
{
    [Ignore]
    [TestFixture]
    class Test_DataMapper
    {
        MockPerson _p;
        Person _person;

        [SetUp]
        public void SetUp()
        {
            _p = new MockPerson();

        }

        [TearDown]
        public void TearDown()
        {
            SetDatabaseTo_SqlServer();
        }

        [Test]
        public void Test_Property_MappedProperties_ForCount()
        {
            Assert.IsTrue(_p.MappedProperties.Count == 4);
        }

        [Test]
        public void Test_Method_GetTableName()
        {
            Assert.IsTrue(_p.GetTableName() == "Personne");
        }

        [Test]
        public void Test_Property_Id()
        {
            Assert.IsTrue(_p.Id == -1);
        }

               

        private static void SetDatabaseTo_MySql()
        {
            // Set the app.config settings to SQL Server DB..
            ConfigFileManager.SetConfigFileDatabaseType("MySql");
            ConfigFileManager.SetConfigFileConnectionStringToKey(ConfigFileManager.KEY_TEST_MYSQL_CONNECTION);
        }

        private static void SetDatabaseTo_SqlServer()
        {
            // Set the app.config settings to SQL Server DB..
            ConfigFileManager.SetConfigFileDatabaseType("SqlServer");
            ConfigFileManager.SetConfigFileConnectionStringToKey(ConfigFileManager.KEY_TEST_SQLSERVER_CONNECTION);
        }


        [Test]
        public void Test_Method_Insert_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            _person.Save();

            Assert.IsTrue(_person.Id > 0);
        }

        [Test]
        public void Test_Method_Update_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            // performs an INSERT..
            _person.Save();

            _person.Name = "TestNameModified";

            // performs an UPDATE..
            _person.Save();


            int generatedId = _person.Id;

            Person p = new Person(generatedId);


            Assert.IsTrue(_person.ToString() == p.ToString());
        }

        [Test]
        public void Test_Method_Delete_ForExistingId_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            // performs an INSERT..
            _person.Save();

            bool result = _person.Delete() &&
                          _person.Id == -1;

            Assert.IsTrue(result);            
        }

        [Test]
        public void Test_Method_Delete_ForNonExistingId_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            Assert.Throws<KeyNotFoundException>(delegate()
            {
                _person.Delete();
            });

        }

        [Test]
        public void Test_Method_Read_ForExistingId_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            _person.Save();
            int lastGeneratedId = _person.Id;

            var p = new Person(lastGeneratedId);

            Assert.AreEqual(_person.ToString(), p.ToString());
        }

        [Test]
        public void Test_Method_Read_ForNonExistingId_SqlServer()
        {
            SetDatabaseTo_SqlServer();

            Assert.Throws<KeyNotFoundException>( delegate()
            {
                // That Id value will never exist..
                new Person(-10);
            });            
        }





        [Test]
        public void Test_Method_Insert_MySql()
        {
            SetDatabaseTo_MySql();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            _person.Save();

            Assert.IsTrue(_person.Id > 0);
        }

        [Test]
        public void Test_Method_Update_MySql()
        {
            SetDatabaseTo_MySql();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            // performs an INSERT..
            _person.Save();

            _person.Name = "TestNameModified";

            // performs an UPDATE..
            _person.Save();


            int generatedId = _person.Id;

            Person p = new Person(generatedId);


            Assert.IsTrue(_person.ToString() == p.ToString());
        }

        [Test]
        public void Test_Method_Delete_ForExistingId_MySql()
        {

            SetDatabaseTo_MySql();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            // performs an INSERT..
            _person.Save();

            bool result = _person.Delete() &&
                          _person.Id == -1;

            Assert.IsTrue(result);
        }

        [Test]
        public void Test_Method_Delete_ForNonExistingId_MySql()
        {
            SetDatabaseTo_MySql();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            Assert.Throws<KeyNotFoundException>(delegate()
            {
                _person.Delete();
            });

        }

        [Test]
        public void Test_Method_Read_ForExistingId_MySql()
        {
            SetDatabaseTo_MySql();

            _person = new Person()
            {
                Name = "TestName",
                FirstName = "TestFirstName",
                YearOfBirth = DateTime.Now.Year
            };

            _person.Save();
            int lastGeneratedId = _person.Id;

            var p = new Person(lastGeneratedId);

            Assert.AreEqual(_person.ToString(), p.ToString());
        }

        [Test]
        public void Test_Method_Read_ForNonExistingId_MySql()
        {
            SetDatabaseTo_MySql();

            Assert.Throws<KeyNotFoundException>(delegate()
            {
                // That Id value will never exist..
                new Person(-10);
            });
        }




    }
}
